package com.cskaoyan.controller;

import com.cskaoyan.bean.Comment;
import com.cskaoyan.bean.Goods;
import com.cskaoyan.bo.CommentBO;
import com.cskaoyan.bo.CreateGoodsBO;
import com.cskaoyan.bo.SearchGoodsBO;
import com.cskaoyan.service.GoodsManagementService;
import com.cskaoyan.vo.*;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @author LiaoLong
 * @date 2021-05-09 14:34
 */
@RestController
@RequestMapping("admin")
@RequiresRoles("超级管理员")
public class GoodsManagementController {

    @Autowired
    private GoodsManagementService goodsManagementService;

    @Value("${file.adminPath}")
    private String fileSavePath;

    @GetMapping("goods/list")
    public BaseRespVo goodsList(SearchGoodsBO searchGoodsBO){
        GoodsVO goodsVO = goodsManagementService.getGoods(searchGoodsBO);
        return BaseRespVo.ok(goodsVO);
    }

    @GetMapping("goods/catAndBrand")
    public BaseRespVo catAndBrand(){
        CatAndBrandVO createGoodsVO = goodsManagementService.catAndBrand();
        return BaseRespVo.ok(createGoodsVO);
    }

    @PostMapping("storage/create")
    public BaseRespVo storageGoodsPic(MultipartFile file){
        StorageVO storageVO = goodsManagementService.fileupload(file, fileSavePath);
        return BaseRespVo.ok(storageVO);
    }

    @PostMapping("goods/create")
    public BaseRespVo createGoods(@RequestBody @Valid CreateGoodsBO createGoodsBO){
        goodsManagementService.createGoods(createGoodsBO);
        return BaseRespVo.ok();
    }

    @PostMapping("goods/delete")
    public BaseRespVo deleteGoods(@RequestBody Goods goods){
        goodsManagementService.deleteGoods(goods);
        return BaseRespVo.ok();
    }

    @GetMapping("goods/detail")
    public BaseRespVo getGoodsDetail(@NotNull Integer id){
        GoodsDetailVO goodsDetailVO = goodsManagementService.getGoodsDetail(id);
        return BaseRespVo.ok(goodsDetailVO);
    }

    @PostMapping("goods/update")
    public BaseRespVo updateGoods(@RequestBody GoodsDetailVO goodsDetailVO){
        goodsManagementService.updateGoods(goodsDetailVO);
        return BaseRespVo.ok();
    }

    @GetMapping("comment/list")
    public BaseRespVo getCommentList(CommentBO commentBO){
        CommentVO commentVO = goodsManagementService.getComments(commentBO);
        return BaseRespVo.ok(commentVO);
    }

    @PostMapping("comment/delete")
    public BaseRespVo deleteComment(@RequestBody Comment comment){
        goodsManagementService.deleteComment(comment);
        return BaseRespVo.ok();
    }
}
